import $http from '@/ajax/http.js';

export const uploading = {
	data() {
		return {
			token: wx.getStorageSync('token'),
			roles: wx.getStorageSync('roles'),
			selectedImage: '',
			uploadFileId: '',
			formData: null,
			boundary: '',
			// 临时存储的图片
			tempFilePaths: ''
		}
	},
	methods: {
		photo() {
			//选取图片
			wx.chooseImage({
				count: 1,
				sizeType: ['original'], //原图  
				sourceType: ['album', 'camera'], //支持选取图片  
				success: (res) => {
					this.tempFilePaths = res.tempFilePaths[0];
					this.selectedImage = res.tempFilePaths[0]
				}
			});
		},

		uploadImage() {
			//上传图片  
			wx.uploadFile({
				url: 'https://5a3a9a24.r24.cpolar.top/common/file/uploadFile',
				filePath: this.tempFilePaths,
				name: 'file', // 根据你的服务器端代码修改这个字段  
				header: {
					'content-type': 'multipart/form-data',
					Authorization: this.token,
				},
				success: (uploadRes) => {
					// 上传成功  
					if (uploadRes.statusCode === 200) {
						const data = JSON.parse(uploadRes.data);
						// 根据服务器返回的数据处理逻辑  
						console.log(data, "上传成功");
						// 显示成功提示给用户  
						wx.showToast({
							title: '图片上传成功',
							icon: 'success',
							duration: 2000
						});
						this.selectedImage = '';
					} else {
						console.log(uploadRes, "上传失败，状态码不是200");
					}
				},
				fail: (uploadError) => {
					// 上传失败  
					console.log(uploadError, "上传失败");
					// 显示失败提示给用户  
					wx.showToast({
						title: '图片上传失败',
						icon: 'none',
						duration: 2000
					});
				}
			});
		}
	}
}